<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/6/20
 * Time: 10:43
 */
class CategoryAction extends CommonAction{
    /*导入夫模板与类库，关于父信息
     * */
    public function index(){
           import('Class.Category',APP_PATH);
         $cate=M('category')->order('sort ASC')->select();
        //  $this->cate=Category::unlimitedforlevel($cate);
         $this->cate=Category::unlimitedforlayer($cate);
        // $this->cate=Category::getparents($cate,2);
        // $this->cate=Category::getchildsid($cate,2);
        // $this->cate=Category::getchilds($cate,2);
        // p($this->cate);
         $this->display();
    }
    /*导入子模板与类库，关于子信息
     * */
    public function child_index(){
         $conditio['pid']=$_GET['pid'];
         $cat=M('category')->where(array('id'=>array('eq',$_GET['pid'])))->find();
         $this->child_cate=M('category')->where($conditio)->order('sort ASC')->select();
         $this->p_name=$cat['name'];
         $this->p_en_name=$cat['en_name'];
         $this->pid=$_GET['pid'];
         $this->display();
    }
    /*排序栏目
     * */
    public function sort_category(){

    $db=M('category');
     $a=0;
    foreach($_POST as $id=>$sort){
           $db->where(array('id'=>$id))->setField('sort',$sort);
        $a=1;
    }
        if($a){
            $this->success('排序成功');
        }else{
            $this->error('排序失败');
        }
    }
    /*添加栏目
     * */
    public function addcategory(){
        //$this->error('对不起请与程序员联系');
        $id=I('pid',0,'intval');
        $cat=M('category')->where(array('id'=>array('eq',$id)))->find();
        $this->p_name=$cat['name'];
        $this->p_en_name=$cat['en_name'];
        $this->pid=$id;
        $this->display();
    }
    /*添加栏目处理
     * */
    public function run_add_category(){
        if($_POST['pid']){
            $u=U(GROUP_NAME.'/Category/child_index',array('pid'=>$_POST['pid'],'p_name'=>$_POST['p_name']));
        }else{
            $u=U(GROUP_NAME.'/Category/index');
         }
        if(M('category')->add($_POST)){
            $this->success('添加成功',$u);
        }else{
            $this->error('添加失败');
        }
    }
    /*导入编辑栏目模板
     * */
    public function editcategory(){
        $data= M('category')->where(array('id'=>array('eq',I('pid'))))->select();
        $this->data=$data;
        $this->display();
    }
    /*处理编辑栏目
     * */
    public function run_edit_category(){

        $data=array(
            'name'=>trim(I('name')),
            'en_name'=>trim(I('en_name')),
            'sort'=>I('sort'),
            'id'=>I('id')
        );
        if($_POST['pid']){
            $u=U(GROUP_NAME.'/Category/child_index',array('pid'=>$_POST['pid'],'p_name'=>$_GET['p_name']));
        }else{
            $u=U(GROUP_NAME.'/Category/index');
        }
        if(M('category')->save($data)){
            $this->success('修改成功',$u);
        }else{
            $this->error('修改失败');
        }
    }
    /*删除栏目
     * */
    public function deletecategory(){

        if(M('category')->where(array('id'=>array('eq',I('pid'))))->delete()){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

}